package com.musiklegal;

import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Subscription extends Activity {

	public void onResume()
	{
		super.onResume();
		if(!MusikLegal.internet(getApplicationContext()))
		{
			
			
			finish();
		}
		
	}
	
	public void onCreate(Bundle savedInstanceState) 
    {
		if(!MusikLegal.internet(getApplicationContext()))
			finish();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.subs);
        
        
        Button start = (Button) this.findViewById(R.id.start);
		Button stop = (Button) this.findViewById(R.id.stop);
		
		if(!MusikLegal.on){
		start.setVisibility(View.VISIBLE);
		stop.setVisibility(View.INVISIBLE);
		}
		else 
			{start.setVisibility(View.INVISIBLE);
			stop.setVisibility(View.VISIBLE);}
		
		start.setOnClickListener(new OnClickListener() {
				
				public void onClick(View arg0) {
					start();
				}
			
			});
		stop.setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {

			}

		});
    
    
    }
	 void start ()
	  {
			
			String username = MusikLegal.mPreferences.getString("UserName", "default");
			String id = ((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
	    	String pass = MusikLegal.mPreferences.getString("PassWord", "default");
	    	
	    	DefaultHttpClient client = new DefaultHttpClient();
	        HttpPost httppost = new HttpPost("http://secure.musiklegal.com/subscription/start_weekly");
	        List< BasicNameValuePair > nvps = new ArrayList< BasicNameValuePair >();
	        nvps.add(new BasicNameValuePair("USERNAME", username));
	        nvps.add(new BasicNameValuePair("UID", id));
	        nvps.add(new BasicNameValuePair("PASSWORD", pass));
	 
	        try {   
	      	  	UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
	            
	            httppost.setEntity(p_entity);
	            
	            HttpResponse response = client.execute(httppost);
	            HttpEntity responseEntity = response.getEntity();
	            

	            SAXParserFactory spf = SAXParserFactory.newInstance();
	            SAXParser sp = spf.newSAXParser();
	            XMLReader xr = sp.getXMLReader();
	            
	            LoginHandler myLoginHandler = new LoginHandler();
	            xr.setContentHandler(myLoginHandler);
	            xr.parse(retrieveInputStream(responseEntity));
	            ParsedLoginDataSet parsedLoginDataSet = myLoginHandler.getParsedLoginData();
	           
	            SharedPreferences.Editor editor=MusikLegal.mPreferences.edit();
                editor.putString("SubId", parsedLoginDataSet.getSiD());

	            
	            if (parsedLoginDataSet.getExtractedString().equals("SUCCESS")) 
	            {
	            	if(parsedLoginDataSet.getSiD()!=null)
                    {
                    	editor.putString("SubId", parsedLoginDataSet.getSiD());
                    	editor.commit();
                    }
	            	MusikLegal.on = true;
	                 finish();
	            } else if(parsedLoginDataSet.getExtractedString().equals("FAIL")) {
	                  Intent intent = new Intent(getApplicationContext(), RegisterMenu.class);
	                  startActivity(intent);
	                   
	            }
	      } catch (Exception e)
	      {
	          
	      }
		  
		 
	  }
	
	void Stop()
	{
		
		String username = MusikLegal.mPreferences.getString("UserName", "default");
		String id = ((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
    	String pass = MusikLegal.mPreferences.getString("PassWord", "default");
    	String subid = MusikLegal.mPreferences.getString("SubId", "default");
    	
    	DefaultHttpClient client = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://topup.musiklegal.com/webservice.php");
        List< BasicNameValuePair > nvps = new ArrayList< BasicNameValuePair >();
        nvps.add(new BasicNameValuePair("USERNAME", username));
        nvps.add(new BasicNameValuePair("UID", id));
        nvps.add(new BasicNameValuePair("PASSWORD", pass));
        nvps.add(new BasicNameValuePair("SUBSCRIPTION_ID", subid));
 
        try {   
      	  	UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
            
            httppost.setEntity(p_entity);
            
            HttpResponse response = client.execute(httppost);
            HttpEntity responseEntity = response.getEntity();
            

            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            
            LoginHandler myLoginHandler = new LoginHandler();
            xr.setContentHandler(myLoginHandler);
            xr.parse(retrieveInputStream(responseEntity));
            ParsedLoginDataSet parsedLoginDataSet = myLoginHandler.getParsedLoginData();

            
            if (parsedLoginDataSet.getExtractedString().equals("SUCCESS")) 
            {
                  
                  startActivity(new Intent(getApplicationContext(), MainMenu.class));
            } else if(parsedLoginDataSet.getExtractedString().equals("FAIL")) {
                  Intent intent = new Intent(getApplicationContext(), RegisterMenu.class);
                  startActivity(intent);
                   
            }
      } catch (Exception e)
      {
          
      }
	  
	 
  }
	
	
	  private InputSource retrieveInputStream(HttpEntity httpEntity) {
	        InputSource insrc = null;
	        try {
	              insrc = new InputSource(httpEntity.getContent());
	        } catch (Exception e) {
	        }
	        return insrc;
	  } 
}
